package fm.quartz;

import com.alibaba.fastjson.JSON;
import fm.entity.RecorderParticipation;
import fm.entityEnum.ExchangeStatus;
import fm.service.RecorderParticipationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类名：fm.quartz.ExchangeStatusQuartz
 * 创建者： CM .
 * 创建时间：2016/3/11
 */
@Component
public class ExchangeStatusQuartz {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExchangeStatusQuartz.class);

    public static boolean RUNNING = false;
    @Autowired
    RecorderParticipationService recorderParticipationService;
    private static final long EXPIRE_TIME = 60 * 60 * 1000;

    public void run() {
        if(RUNNING){
            return;
        }
//        LOGGER.debug("exchange status auto expire task start working ...");
        RUNNING = true;
        try {
            List<RecorderParticipation> recorders = recorderParticipationService.getAllNotExchangeRecorder();
            for (RecorderParticipation rp : recorders) {
                long restTime = System.currentTimeMillis() - rp.getCreateTime().getTime();
                if (restTime >= EXPIRE_TIME) {
                    //1小时 超过1小时，视为失效
                    rp.setExchangeStatus(ExchangeStatus.EXPIRED);
                    recorderParticipationService.updateRecorder(rp);
                    LOGGER.info("recorder exchange expire :{}", rp.getId());
                }
            }
        } catch (Exception ex) {
            LOGGER.error("exchange status auto expire task occur error:", ex);
        }
        RUNNING = false;
//        LOGGER.debug("exchange status auto expire task finish working ...");
    }
}
